// 引入 css 文件
require('../css/index.less')
require("../fonts/iconfont.css")
require("../lib/swiper/swiper-bundle.css")
require("../lib/swiper/swiper-bundle.js")

window.$utils.checkLogin();


//调用公共的foot组键
document.ready(function(){
    window.$utils.tabs(0);

    let exercise=document.querySelector(".exercise")
    exercise.addEventListener("click",function(){
        location.href="./database.html"
    })


    new Swiper ('.swiper-container', {
        autoplay:true,
        loop: true, // 循环模式选项
        autoplay:{
            disableOnInteraction: false,
          },
    })

    let drill=document.querySelector(".drill")
    drill.addEventListener("click",function(){
        location.href="./exercise.html"
    })
})

//请求首页数据
const userId=JSON.parse(window.localStorage.getItem('user')).userId;
const rankBox=document.querySelector("#rank")
const daysBox=document.querySelector("#days")
let on=document.querySelector("#on");

window.$http.get('/headPageInfo?userId='+userId,function(res){
    console.log(res);

    //动态渲染数据
    rankBox.textContent=res.data.rank;
    daysBox.textContent=res.data.punchIn;

    //只有当res.data.isPunch为false
    if(res.data.isPunch==='true'){
        on.className="clock";
        on.textContent="已打卡"
    }else{
        on.addEventListener("click",punchIn)
    }
})



//今日打卡


function punchIn(){
    //发请求
    window.$http.get('/clockIn?userId='+userId,function(res){
        if(res.status===0){
            //成功之后，更改打卡天数，阻止事件的再次触发
            daysBox.textContent=daysBox.textContent-0+1
            on.className="clock";
            on.textContent="已打卡"
            on.removeEventListener("click",punchIn)
        }
    })
}